package com.ihome.android.mod;

import java.net.HttpURLConnection;
import java.net.URL;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.ihome.android.util.log.LogUtil;

public class NetWorkMod {
	private static final String	TAG	= NetWorkMod.class.getSimpleName();
	private static final String	URL	= "http://www.baidu.com";

	public static boolean checkNetWorkValiableAndWait(Context ctx) {
		boolean ret = false;
		ConnectivityManager connectivityManager = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();

		if (activeNetInfo != null) {
			LogUtil.d(TAG, "Active Network Type : " + activeNetInfo.getTypeName());

			try {
				URL url = new URL(URL);
				HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
				urlc.setConnectTimeout(5000);
				urlc.connect();

				if (urlc.getResponseCode() == 200) {
					ret = true;
				}

			} catch (Exception e) {
				LogUtil.d(TAG, "can't Reachable:" + URL);
			}

			return ret;
		}
		else {
			LogUtil.d(TAG, "Network closed");
		}

		return ret;
	}
}
